﻿#include "opdb.h"
#include <QMessageBox>
#include <QDebug>
OpDb::OpDb(QObject *parent) : QObject(parent)
{
    m_db = QSqlDatabase::addDatabase("QMYSQL");
}

OpDb::~OpDb()
{
    m_db.close();
}

OpDb &OpDb::getInstance()
{
    static OpDb opdb;
    return opdb;
}

void OpDb::connectDB()
{
    m_db.setHostName("127.0.0.1");
    m_db.setUserName("root");
    m_db.setPassword("12345678");
    m_db.setDatabaseName("chat");
    bool is = m_db.open();//连接
    if(is)
       qDebug()<<"connected DB success";
    else
        qDebug()<<"连接失败";
}

bool OpDb::addUser(userInfo u,int& id,const char* buff)
{
    if(u.name==NULL||u.pwd==NULL)
        return false;
    QTime randtime;
    randtime = QTime::currentTime();
    qsrand(randtime.msec()+randtime.second()*1000);
    int num = qrand()%1000000+1000000;
    QSqlQuery query;
    QString sql = QString("insert into userInfo(id,name,pwd,online) values('%1','%2','%3',0)").arg(num).arg(u.name).arg(u.pwd);
    query.exec(sql);
    id = num;
    sql = QString("insert into userInfo2(id,nickname,headurl) values('%1','%2',':/res/head1.jpg') ").arg(num).arg(u.name);
    query.exec(sql);
    sql = QString("insert into secure values('%1','%2')").arg(num).arg(buff);
    return query.exec(sql);
}

bool OpDb::selectUser(userInfo u)//查看用户是否在线
{
    if(u.name==NULL)
        return false;
    QSqlQuery query;
    QString sql = QString("select* from userInfo where id=\'%1\' and online = 0").arg(u.name);
    query.exec(sql);
    bool b = query.next();

    if(b)
    {
        QString sql = QString("update userInfo set online=1 where id=\'%1\' ").arg(u.name);
        query.exec(sql);
    }
    return b;
}

void OpDb::handlerLogOut(userInfo u)
{
    if(u.name==NULL)
        return;
    QSqlQuery query;
    QString sql = QString("update userInfo set online=0 where id=\'%1\'").arg(u.name);
    query.exec(sql);
}

int OpDb::isOnline(QString uid)
{
    QSqlQuery query;
    QString sql = QString("select online from userInfo where id=\'%1\'").arg(uid);
    query.exec(sql);
    query.next();
    return query.value(0).toInt();
}

QList<userInfo> OpDb::selectOnlineFriend(userInfo u)
{
    if(u.name==NULL||u.pwd==NULL)
        return {};
    QSqlQuery query;
    QSqlQuery query2;
    QString sql = QString("select friendId from friend where id=\'%1\'").arg(u.name);
    query.exec(sql);
    QList<userInfo> listU;
    while(query.next())
    {
        userInfo temp;
        QString str = query.value(0).toString();
        strcpy(temp.name,str.toStdString().c_str());
        listU.append(temp);
    }
    for(int i = 0;i<listU.size();++i)
    {
        QString sql2 = QString("select online from userInfo where id=\'%1\'").arg(listU.at(i).name);
        query2.exec(sql2);
        query2.next();
        listU[i].online = query2.value(0).toInt();
    }

    return listU;
}

QList<QString> OpDb::selectGroup(QString uname)
{
    QList<QString> list;
    QString sql = QString("select DISTINCT gid from group2 where uid= '%1'").arg(uname);
    QSqlQuery query;
    query.exec(sql);
    while(query.next())
    {
        QString str = query.value(0).toString();
        list.append(str);
    }
    return list;
}

bool OpDb::handleAddFriend(const char *name)//查询此人是否在线
{
    if(name==NULL)
        return false;
    QSqlQuery query;
    QString sql = QString("select* from userInfo where id=\'%1\' and  online = 1").arg(name);
    query.exec(sql);
    bool b = query.next();
    return b;
}

void OpDb::addFriend(const char *name1, const char *name2)
{
    if(name1==NULL || name2 == NULL)
        return;
    QSqlQuery query;
    QString sql = QString("INSERT INTO friend(id,friendId) SELECT \'%1\',\'%2\' from dual where not exists (SELECT id FROM friend where id = \'%3\' and friendId = \'%4\')").arg(name1).arg(name2).arg(name1).arg(name2);
    query.exec(sql);
    sql.clear();
    sql = QString("INSERT INTO friend(id,friendId) SELECT \'%1\',\'%2\' from dual where not exists (SELECT id FROM friend where id = \'%3\' and friendId = \'%4\')").arg(name2).arg(name1).arg(name2).arg(name1);
    query.exec(sql);
}



QString OpDb::getUserInfo(QString name)
{
    QString sql = "select * from userinfo2 where id='"+name+"'";
    QSqlQuery query;
    query.exec(sql);
    QString list;
    if(query.next())
    {
        for(int i=0;i<13;++i)
        {
            list += query.value(i).toString()+"#";
        }
    }
    return list;
}

QString OpDb::getUserHead(QString name)
{
    QString sql = "select headurl from userinfo2 where id='"+name+"'";
    QSqlQuery query;
    query.exec(sql);
    query.next();
    qDebug()<<sql;
    qDebug()<<query.value(0).toString();
    return query.value(0).toString();
}

bool OpDb::ResetPwd(PDU *pdu)
{
    if(pdu==NULL)
        return false;
    QSqlQuery query;
    char name[15]={0};
    char pwd[20]={0};
    memcpy(name,pdu->friendId,12);
    strncpy(pwd,pdu->friendId+13,19);
    QString sql = QString("update userInfo set pwd= \'%1\' where id=\'%2\' ").arg(pwd).arg(name);
    if(query.exec(sql))
        return true;
    return false;
}

QString OpDb::handlerFindPwd(QString username)
{
    if(username.isEmpty())
        return "";
    QString sql = QString("select anser from secure where id = \'%1\'").arg(username);
    QSqlQuery query;
    query.exec(sql);
    if(query.next())
    {
        QString anser = query.value(0).toString();
        return anser;
    }
    return "";
}

bool OpDb::registerGroup(QString groupname,QString& groupid,userInfo& u)
{
    QTime randtime;
    randtime = QTime::currentTime();
    qsrand(randtime.msec()+randtime.second()*1000);
    int num = qrand()%1000000+3000000;
    groupid = QString("%1").arg(num);
    QString sql = QString("insert into group1(groupid,groupname,grouphead) values('%1','"+groupname+"','D:/ChatClientInfo/head/h6.jpg')").arg(num);
    qDebug()<<sql<<endl;
    QSqlQuery query;
    query.exec(sql);
    sql = QString("insert into group2(gid,uid) values('%1','%2')").arg(num).arg(u.pwd);
    return query.exec(sql);
}

QString OpDb::getGroupHead(QString groupid)
{
    QString sql = "select grouphead from group1 where groupid='"+groupid+"'";
    QSqlQuery query;
    query.exec(sql);
    if(query.next())
    {
        QString anser = query.value(0).toString();
        return anser;
    }
    return "";
}

QString OpDb::getGroupName(QString groupid)
{
    QString sql = "select groupname from group1 where groupid='"+groupid+"'";
    QSqlQuery query;
    query.exec(sql);
    if(query.next())
    {
        QString anser = query.value(0).toString();
        return anser;
    }
    return "";
}

void OpDb::addFriendToGroup(QString uid, QString gid)
{
    if(uid.isEmpty() || gid.isEmpty())
        return;
    QSqlQuery query;
    QString sql = QString("INSERT INTO group2 SELECT \'%1\',\'%2\' from dual where not exists (SELECT * FROM group2 where uid = \'%3\' and gid = \'%4\')").arg(uid).arg(gid).arg(uid).arg(gid);
    query.exec(sql);
}

QList<QPair<QString,int>> OpDb::getGroupPerson(QString gid)
{
    QList<QPair<QString,int>> list;
    QString sql = QString("select uid from group2 where gid='%1'").arg(gid);
    QSqlQuery query;
    query.exec(sql);
    while(query.next())
    {
        QString anser = query.value(0).toString();
        QString headurl = getUserHead(anser);
        int n = isOnline(anser);
        anser = anser + "&" + gid + "&" +headurl;
        list.append(qMakePair<QString,int>(anser,n));
    }
    return list;
}

void OpDb::updateGroupNotify(QString msg, QString gid)
{
    QString sql = QString("update group1 set groupnotice = '%1' where groupid='%2'").arg(msg).arg(gid);
    QSqlQuery query;
    query.exec(sql);
}
